package com.qudian.java.core.common.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.math.BigDecimal;


/**
 * 等额本息计算
 * @author qufenqi
 *
 */
public class RefundUtils {

    /**
     * 年化利率转换为月利率、日利率、固定天数利率
     * @param rate  年化利率 必传项   获取月利率时传参，其他传 null
     * @param days 天数  获取日利率时传参，其他传 null
     * @param repayperiod 还款周期   获取固定天数利率时传参
     * @return
     */
    public static double getRate(double rate,Integer days,Integer repayperiod){
    	double res=0d;
    	if(ObjectUtil.isNull(days)&&ObjectUtil.isNull(repayperiod)){
    		res= rate/12;
    	}
    	if(ObjectUtil.isNotNull(days)&&days>0&&ObjectUtil.isNull(repayperiod)){
    		res= rate/days;
    	}
    	if(ObjectUtil.isNotNull(days)&&days>0&&ObjectUtil.isNotNull(repayperiod)&&repayperiod>0){
    		res= rate/days*repayperiod;
    	}
        return res;
    }
    
    
   
    /**
    *
    * 等额本息还款【利息多】
    * @param totalCapital 总本金
    * @param rate  利率
    * @param periods  贷款期数
    */
   public static String averageCapitalInterest(BigDecimal totalCapital,double rate,int periods){
	   JSONArray resj=new JSONArray();
       /**
        * 月还款本息
        */
	   // 每月本息金额  = (本金×月利率×(1＋月利率)＾还款月数)÷ ((1＋月利率)＾还款月数-1)
	   BigDecimal fenzi=totalCapital.multiply(new BigDecimal(Double.toString(rate)))
			   	.multiply(new BigDecimal(Double.toString(Math.pow((1+rate),periods))));
	   BigDecimal fenmu=new BigDecimal(Double.toString(Math.pow((1+rate),periods)-1));
	   BigDecimal res = fenzi.divide(fenmu,30,BigDecimal.ROUND_HALF_UP);
       for(int i=1;i<periods+1;i++){
    	   // 每月本金 = 本金×月利率×(1+月利率)^(还款月序号-1)÷((1+月利率)^还款月数-1)
//    	   (totalCapital* rate * (Math.pow((1+rate),i-1))))
    	   BigDecimal tempfenzi = totalCapital.multiply(new BigDecimal(Double.toString(rate)))
    	    	     .multiply(new BigDecimal(Double.toString(Math.pow((1+rate),i-1))));
    	   BigDecimal tempfenmu = new BigDecimal(Double.toString((Math.pow(1+rate,periods)-1)));
    	   BigDecimal res1 = tempfenzi.divide(tempfenmu,30,BigDecimal.ROUND_HALF_UP);
    	   JSONObject tem=new JSONObject();
    	   tem.put("period", i);
    	   tem.put("monthtotal", res.toString());
    	   tem.put("capital", res1.toString());
    	   tem.put("interest", res.subtract(res1).toString());
           resj.add(tem);
       }
       return resj.toString();
   }
   
    /**
     * TODO
     * @param args
     */
//    public static void main(String[] args) {
//        BigDecimal totalCapital=new BigDecimal(1495.760000000005025753600000017732);
//        double rate=0.1;
//        int periods=1;
//        String res = RefundUtils.averageCapitalInterest(totalCapital, RefundUtils.getRate(rate, null, null), periods);
//        System.out.println(totalCapital.multiply(new BigDecimal(Double.toString(RefundUtils.getRate(rate, 365, null)))).multiply(new BigDecimal(30)));
//        JSONArray arr= JSONArray.fromObject(res);
////        System.out.println(arr);
//        JSONObject obj = arr.getJSONObject(0);
////        System.out.println(new BigDecimal(obj.get("monthtotal").toString()));
//        //        ReFundResultsVO json =(ReFundResultsVO) JSONObject.toBean(JSONObject.fromObject(res), ReFundResultsVO.class);
//		for(int i=0;i<arr.size();i++){
//			JSONObject json = JSONObject.fromObject(arr.get(i));
//			int period = (int) json.get("period");
//			System.out.println(period+"利息："+new BigDecimal(json.get("interest").toString()));
//			System.out.println(period+"月付"+new BigDecimal(json.get("monthtotal").toString()));
//			System.out.println(period+"本金"+new BigDecimal(json.get("capital").toString()));
//		}
//    }
}
